package clustering;
import java.io.*;
import java.util.*;
public class RandomClusters_Generation {
	private List<Integer> cluster_size;
	private List<String> Idlist;
	
	public List<Integer> GetCluster_Size(String filename){
		cluster_size = new ArrayList<Integer>();
		File f = new File(filename);
		FileInputStream fis;
		try{
			fis = new FileInputStream(f);
			InputStreamReader isr=new InputStreamReader(fis);
			BufferedReader br=new BufferedReader(isr);
			String line = br.readLine();
			while(line != null){
				line = line.trim();
				if(line.length()>0){
					int index = line.indexOf("\t");
					cluster_size.add(Integer.valueOf(line.substring(0,index)));
				}
				line = br.readLine();
			}
			br.close();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
		return cluster_size;
	}
	
	public void ReadIdList(String filename){
		Idlist = new ArrayList<String>();
		File f = new File(filename);
		FileInputStream fis;
		try{
			fis = new FileInputStream(f);
			InputStreamReader isr=new InputStreamReader(fis);
			BufferedReader br=new BufferedReader(isr);
			String line = br.readLine();
			while(line != null){
				line = line.trim();
				if(line.length()>0){
					Idlist.add(line);
				}
				line = br.readLine();
			}
			br.close();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void GenerateClusters(String random_cluster_file){
		File f = new File(random_cluster_file);
		OutputStreamWriter write;
		try{
			write = new OutputStreamWriter(new FileOutputStream(f,false));
			BufferedWriter bw =new BufferedWriter(write);
			HashMap<Integer,Integer> visited = new HashMap<Integer,Integer>();
			for(int size: cluster_size){
				bw.write(String.valueOf(size));
				Random r = new Random();
				int count = 0;
				while(count<size){
					int index = r.nextInt(Idlist.size());
					if(visited.containsKey(index))
						continue;
					visited.put(index, visited.size());
					count++;
					bw.write("\t"+Idlist.get(index));
				}
				bw.newLine();
			}
			bw.close();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}
